﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServerDALDll;

namespace bChatServerDAL
{
    public static class ServerDAL
    {
        
        private static ServerDataContext _context;

        public static void Init()
        {
            try
            {

                _context = new ServerDataContext();

                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DAL initialized",
                    Ex = null,
                    Operation = "Init",
                    Type = "Notify"

                });

            }
            catch (Exception ex)
            {
                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DAL init failed",
                    Ex =ex,
                    Operation ="DAL ctor",
                    Type="Fatal error"

                });
            }
        }

        public static bool IsUserExist( string username )
        {
            return GetUserBy( username ) != null;
        }

        public static bool IsEmailUnique( string email )
        {
            return GetUsers().Count( x => x.Email == email ) == 0;
        }

        public static bool RegisterUser( string username, string email, string pass, string description )
        {
            if (IsUserExist( username ) || !IsEmailUnique( email ) || String.IsNullOrEmpty( pass ))
                return false;

            User user = new User();
            user.UserName = username;
            user.Email = email;
            user.Password = pass;
            user.Description = description;

            _context.Users.InsertOnSubmit( user );
            _context.SubmitChanges();

            return true;
        }

        public static User GetUserBy( string username )
        {
            try
            {

                return _context.Users.FirstOrDefault(x => x.UserName == username);

            }
            catch (Exception ex)
            {
                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DB query failed",
                    Ex = ex,
                    Operation = "GetUserBy",
                    Type = "Error"

                });

                return null;
            }
        }

        public static List<User> GetUsers()
        {
            try
            {
                return _context.Users.ToList();
            }
            catch (Exception ex)
            {
                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DB query failed",
                    Ex = ex,
                    Operation = "GetUsers",
                    Type = "Error"

                });

                return new List<User>();
            }
        }

        public static Channel GetChannelBy(string channelName)
        {
            try
            {

                return _context.Channels.FirstOrDefault(x => x.ChannelName == channelName);

            }
            catch (Exception ex)
            {
                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DB query failed",
                    Ex = ex,
                    Operation = "GetChannelBy",
                    Type = "Error"

                });

                return null;
            }
        }

        public static Channel GetChannelBy( int channelId )
        {
            try
            {

                return _context.Channels.FirstOrDefault(x => x.ChannelId == channelId);

            }
            catch (Exception ex)
            {
                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DB query failed",
                    Ex = ex,
                    Operation = "GetChannelBy",
                    Type = "Error"

                });

                return null;
            }
        }

        public static List<Channel> GetChannels()
        {
            try
            {

                return _context.Channels.ToList();

            }
            catch (Exception ex)
            {
                ServerDALNotifyEvent(null, new ServerDALNotifyEventArgs
                {
                    Comment = "DB query failed",
                    Ex = ex,
                    Operation = "GetChannels",
                    Type = "Error"

                });

                return new List<Channel>();
            }
        }




        public static event ServerDALNotifyHandler ServerDALNotifyEvent;

        public delegate void ServerDALNotifyHandler(object sender, ServerDALNotifyEventArgs e);

    }
}